package com.example.restaurantclient.Fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;

import com.anshi.shopchangepro.MainActivity;
import com.anshi.shopchangepro.selfview.ListViewForScrollView;
import com.bumptech.glide.Glide;
import com.example.restaurantclient.Activity.AppointmentListActivity;
import com.example.restaurantclient.Activity.QueueUpListActivity;
import com.example.restaurantclient.Activity.SearchActivity;
import com.example.restaurantclient.Adapter.HotDishAdapter;
import com.example.restaurantclient.Adapter.NewDishAdapter;
import com.example.restaurantclient.Bubble.GridViewForScrollView;
import com.example.restaurantclient.Entities.Dish;
import com.example.restaurantclient.Entities.Variable;
import com.example.restaurantclient.R;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.zaaach.citypicker.CityPickerActivity;

import java.util.ArrayList;
import java.util.List;



public class HomeFragment extends Fragment {
    private View view;
    //轮播图
    private Banner mBanner;
    private LocalImageLoader mImageLoader;
    private ArrayList<Integer> localImagePath;
    private ArrayList<String> onlineImagePath;
    private ArrayList<String> imageTitle;
    private TextView cityView;
    private LinearLayout cityLinearLayout;
    private static final int REQUEST_CODE_PICK_CITY = 0;
    private SearchView searchView;
    private LinearLayout queueClick;//排队取号
    private LinearLayout appointmentClick;//预约排号
    private LinearLayout orderClick;//点餐
    private GridViewForScrollView gridViewNew;
    private ListViewForScrollView listViewHot;
    private List<Dish> dishesNew=new ArrayList<>();
    private List<Dish> dishesHot=new ArrayList<>();
    private NewDishAdapter gridAdapter;
    private HotDishAdapter hotDishAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view==null) {
            view = inflater.inflate(R.layout.fragment_home, container, false);
            Variable.initData();
            intDate();
            intView();
            findNewDish();
            findHotDish();
            InitDish();

            MyClickListener myClickListener=new MyClickListener();
            //定位控件
            cityLinearLayout.setOnClickListener(myClickListener);
            //搜索控件
            searchView.setOnClickListener(myClickListener);
            queueClick.setOnClickListener(myClickListener);
            appointmentClick.setOnClickListener(myClickListener);
            orderClick.setOnClickListener(myClickListener);

        }
        return view;
    }

    private void findHotDish() {
        //进行网络请求
       dishesHot.addAll(Variable.dishes.subList(6,12));
    }

    private void findNewDish() {
        //进行网络请求
        dishesNew.addAll(Variable.dishes.subList(0,6));

    }

    private void InitDish() {
        Log.e("dishesNew",dishesNew.size()+"");
        gridAdapter=new NewDishAdapter(getContext(),dishesNew,R.layout.item_gridview_newdish);
        gridViewNew.setAdapter(gridAdapter);
        hotDishAdapter=new HotDishAdapter(getContext(),dishesHot,R.layout.item_listview_hotdish);
        listViewHot.setAdapter(hotDishAdapter);

    }

    //监听器类
    class MyClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.home_city_linear:
                    startActivityForResult(new Intent(getContext(), CityPickerActivity.class),
                            REQUEST_CODE_PICK_CITY);
                    break;
                case R.id.sv_search://点击搜索
                    startActivity(new Intent(getContext(), SearchActivity.class));
                    break;
                case R.id.btn_rowNumber://点击排号
                    startActivity(new Intent(getContext(), QueueUpListActivity.class));
                    break;
                case R.id.btn_order:
                    startActivity(new Intent(getContext(), MainActivity.class));
                    break;
                case R.id.btn_appointment:
                    startActivity(new Intent(getContext(), AppointmentListActivity.class));
                    break;
            }
        }
    }
    //返回选中城市
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == -1){
            if (data != null){
                String city = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY);
//                if (!city.equals("全国")) {
//                    city += "市";
//                }
                cityView.setText(city);
            }
        } else {

        }
    }
    @Override
    public void onStart() {

        super.onStart();
    }

    /**
     * 图片加载类
     */
    private class LocalImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }
    //轮播图方法
    private void intDate() {
        localImagePath = new ArrayList<>();
        imageTitle = new ArrayList<>();
        onlineImagePath=new ArrayList<>();
        //本地图片
//        imagePath.add(R.mipmap.ic_launcher);
//        imagePath.add(R.mipmap.ic_launcher);
//        imagePath.add(R.mipmap.ic_launcher);
//        imagePath.add(R.mipmap.ic_launcher);
//        imagePath.add(R.mipmap.ic_launcher);
//        imageTitle.add("第一");
//        imageTitle.add("第二");
//        imageTitle.add("第三");
//        imageTitle.add("第四");
//        imageTitle.add("第五");
        //网络图片
        onlineImagePath.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091321%2Fgzsdxzdook2gzsdxzdook2.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644567620&t=7c29ed2b7878c18100dda077e7313b57");
        onlineImagePath.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180725%2F03a330ab88c34949a50dd2b1c843e35e.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644567962&t=570c7ef7314f95c1491a2a01ab832361");
        onlineImagePath.add("https://img2.baidu.com/it/u=2542162739,1371052520&fm=253&fmt=auto&app=138&f=JPEG?w=751&h=500");
        onlineImagePath.add("https://img0.baidu.com/it/u=1014442451,798959241&fm=253&fmt=auto&app=138&f=JPEG?w=700&h=468");
        onlineImagePath.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F969484950f7ac39e7334eb6f564f149f3a7abf0154abb-K1kP2P_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644568062&t=456c951b74aa367dad4398c71c5c89a8");
        onlineImagePath.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01e84757675e660000012e7eaeb4b8.jpg%402o.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644568062&t=33dcfced483070595e1c17bab4b61b4b");


        imageTitle.add("柠檬焗肉");
        imageTitle.add("鸡腿菇");
        imageTitle.add("红酒牛排");
        imageTitle.add("蔬菜沙拉");
        imageTitle.add("鳕鱼卷");
        imageTitle.add("玉子虾仁");
    }
    //轮播图方法
    private void intView() {
        mImageLoader = new LocalImageLoader();
        mBanner = view.findViewById(R.id.banner);
        //样式
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //加载器
        mBanner.setImageLoader(mImageLoader);
        //动画效果
        mBanner.setBannerAnimation(Transformer.ZoomOutSlide);
        //图片标题
        mBanner.setBannerTitles(imageTitle);
        //间隔时间
        mBanner.setDelayTime(4500);
        //是否为自动轮播
        mBanner.isAutoPlay(true);
        //图片小点显示所在位置
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //图片加载地址
        //本地图片
//        mBanner.setImages(localImagePath);
        //网络图片
        mBanner.setImages(onlineImagePath);
        //启动轮播图。
        mBanner.start();
        //监听轮播图
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getContext(),"点击了"+position+"张图片",Toast.LENGTH_SHORT).show();
            }
        });

        //初始化控件
        mBanner=view.findViewById(R.id.banner);
        cityView=view.findViewById(R.id.home_city);
        searchView=view.findViewById(R.id.sv_search);
        orderClick=view.findViewById(R.id.btn_order);
        queueClick=view.findViewById(R.id.btn_rowNumber);
        appointmentClick=view.findViewById(R.id.btn_appointment);
        gridViewNew=view.findViewById(R.id.home_gridview_new);
        listViewHot=view.findViewById(R.id.home_lv_hot);
        cityLinearLayout=view.findViewById(R.id.home_city_linear);


    }

}

